Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    currentSemester: {
      type: String,
      value: '2025年上学期'
    },
    currentWeek: {
      type: String,
      value: '2025年10月第1周'
    }
  },

  data: {
    selectedDay: 'monday',
    showCourseEdit: false,
    editingCourse: {
      name: '',
      color: '#e3f2fd',
      day: '',
      slot: -1
    },
    courseColors: [
      '#e3f2fd', // 浅蓝色
      '#f3e5f5', // 浅紫色
      '#e8f5e8', // 浅绿色
      '#fff3e0', // 浅橙色
      '#fce4ec', // 浅粉色
      '#f1f8e9', // 浅青绿色
      '#fff8e1', // 浅黄色
      '#e0f2f1'  // 浅青色
    ],
    timeSlots: [
      { number: 1, startTime: '08:30', endTime: '09:15' },
      { number: 2, startTime: '09:25', endTime: '10:10' },
      { number: 3, startTime: '10:20', endTime: '11:05' },
      { number: 4, startTime: '11:15', endTime: '12:00' },
      { number: 5, startTime: '14:00', endTime: '14:45' },
      { number: 6, startTime: '14:55', endTime: '15:40' },
      { number: 7, startTime: '15:50', endTime: '16:35' },
      { number: 8, startTime: '16:45', endTime: '17:30' }
    ],
    weekDays: [
      {
        day: 'monday',
        name: '周一',
        courses: [
          { name: '语文', color: '#e3f2fd' },
          null,
          null,
          null,
          null,
          null,
          null,
          null
        ]
      },
      {
        day: 'tuesday',
        name: '周二',
        courses: [
          null,
          { name: '数学', color: '#f3e5f5' },
          null,
          null,
          null,
          null,
          null,
          null
        ]
      },
      {
        day: 'wednesday',
        name: '周三',
        courses: [null, null, null, null, null, null, null, null]
      },
      {
        day: 'thursday',
        name: '周四',
        courses: [null, null, null, null, null, null, null, null]
      },
      {
        day: 'friday',
        name: '周五',
        courses: [null, null, null, null, null, null, null, null]
      }
    ]
  },

  methods: {
    // 点击遮罩层关闭弹窗
    onOverlayTap() {
      this.triggerEvent('close');
    },

    // 点击弹窗内容阻止冒泡
    onModalTap() {
      // 阻止事件冒泡
    },

    // 设置按钮
    onSettings() {
      wx.showToast({
        title: '课表设置功能开发中',
        icon: 'none'
      });
    },

    // 上一周
    onPrevWeek() {
      this.triggerEvent('prevWeek');
    },

    // 下一周
    onNextWeek() {
      this.triggerEvent('nextWeek');
    },

    // 选择星期
    onSelectDay(e) {
      const day = e.currentTarget.dataset.day;
      this.setData({
        selectedDay: day
      });
    },

    // 点击课程格子
    onCourseTap(e) {
      const { day, slot } = e.currentTarget.dataset;
      const weekDay = this.data.weekDays.find(wd => wd.day === day);
      const course = weekDay.courses[slot];
      
      this.setData({
        showCourseEdit: true,
        editingCourse: {
          name: course ? course.name : '',
          color: course ? course.color : '#e3f2fd',
          day: day,
          slot: slot
        }
      });
    },

    // 添加行
    onAddRow() {
      const timeSlots = this.data.timeSlots;
      const newSlotNumber = timeSlots.length + 1;
      const lastSlot = timeSlots[timeSlots.length - 1];
      
      // 计算新时间段的开始时间（上一时段的结束时间）
      const lastEndTime = lastSlot.endTime;
      const [lastHour, lastMinute] = lastEndTime.split(':').map(Number);
      const newStartMinute = lastMinute + 10; // 课间休息10分钟
      const newStartHour = lastHour + Math.floor(newStartMinute / 60);
      const newStartMinuteFinal = newStartMinute % 60;
      const newEndMinute = newStartMinuteFinal + 45; // 每节课45分钟
      const newEndHour = newStartHour + Math.floor(newEndMinute / 60);
      const newEndMinuteFinal = newEndMinute % 60;
      
      const newStartTime = `${newStartHour.toString().padStart(2, '0')}:${newStartMinuteFinal.toString().padStart(2, '0')}`;
      const newEndTime = `${newEndHour.toString().padStart(2, '0')}:${newEndMinuteFinal.toString().padStart(2, '0')}`;
      
      const newTimeSlot = {
        number: newSlotNumber,
        startTime: newStartTime,
        endTime: newEndTime
      };
      
      const weekDays = this.data.weekDays.map(day => ({
        ...day,
        courses: [...day.courses, null]
      }));
      
      this.setData({
        timeSlots: [...timeSlots, newTimeSlot],
        weekDays: weekDays
      });
      
      wx.showToast({
        title: '添加成功',
        icon: 'success'
      });
    },

    // 重新录入
    onReset() {
      wx.showModal({
        title: '确认重新录入',
        content: '确定要清空所有课程安排吗？',
        success: (res) => {
          if (res.confirm) {
            const weekDays = this.data.weekDays.map(day => ({
              ...day,
              courses: day.courses.map(() => null)
            }));
            
            this.setData({
              weekDays: weekDays
            });
            
            wx.showToast({
              title: '已清空',
              icon: 'success'
            });
          }
        }
      });
    },

    // 确定保存
    onConfirm() {
      this.triggerEvent('save', {
        weekDays: this.data.weekDays,
        timeSlots: this.data.timeSlots
      });
      
      wx.showToast({
        title: '保存成功',
        icon: 'success'
      });
      
      setTimeout(() => {
        this.triggerEvent('close');
      }, 1500);
    },

    // 课程编辑相关方法
    onCourseEditTap() {
      this.setData({
        showCourseEdit: false
      });
    },

    onCourseEditContainerTap() {
      // 阻止事件冒泡
    },

    onCloseCourseEdit() {
      this.setData({
        showCourseEdit: false
      });
    },

    onCourseNameInput(e) {
      this.setData({
        'editingCourse.name': e.detail.value
      });
    },

    onSelectColor(e) {
      const color = e.currentTarget.dataset.color;
      this.setData({
        'editingCourse.color': color
      });
    },

    onCancelEdit() {
      this.setData({
        showCourseEdit: false
      });
    },

    onSaveCourse() {
      const { name, color, day, slot } = this.data.editingCourse;
      
      if (!name.trim()) {
        wx.showToast({
          title: '请输入课程名称',
          icon: 'none'
        });
        return;
      }
      
      const weekDays = this.data.weekDays.map(wd => {
        if (wd.day === day) {
          const newCourses = [...wd.courses];
          newCourses[slot] = { name: name.trim(), color };
          return { ...wd, courses: newCourses };
        }
        return wd;
      });
      
      this.setData({
        weekDays: weekDays,
        showCourseEdit: false
      });
      
      wx.showToast({
        title: '保存成功',
        icon: 'success'
      });
    }
  }
});

